//
//  XZMyselfHeaderView.m
//  Teecloud
//
//  Created by 贵州贵农网络科技有限公司 on 2018/7/19.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMyselfHeaderView.h"
#import "XZInfoModel.h"
#import "GMlevelView.h"
@interface XZMyselfHeaderView ()

@property (nonatomic, strong) UIView       *areaView;//背景view
@property (nonatomic, strong) UIImageView  *backImg;//背景图片
@property (nonatomic, strong) UIImageView  *iconImageView;//头像
@property (nonatomic, strong) UILabel      *nameLabel;//名称
@property (nonatomic, strong) GMlevelView  *levelView;//等级
@property (nonatomic, strong) UIImageView  *gradeImageView;//等级图

@property (nonatomic, strong) UILabel      *pointLabel;//分数标签
@property (nonatomic, strong) UILabel      *scoreLabel;//分数
@property (nonatomic, strong) UIButton     *signInButton;//签到按钮
@property (nonatomic, strong) UILabel      *gradeNameLabel;//等级名称

@end


@implementation XZMyselfHeaderView

#pragma mark -  initWithFrame
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.backImg];
        [self addSubview:self.areaView];
        [self addSubview:self.iconImageView];
        [self.areaView addSubview:self.nameLabel];
        [self.areaView addSubview:self.levelView];
        
        [self.areaView addSubview:self.pointLabel];
        [self.areaView addSubview:self.scoreLabel];
        [self.areaView addSubview:self.signInButton];
        [self.areaView addSubview:self.gradeNameLabel];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goInfoController)];
        self.iconImageView.userInteractionEnabled = YES;
        [self.iconImageView addGestureRecognizer:tap];
    }
    return self;
}
#pragma mark - layoutSubviews
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //背景图片
    [self.backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).offset(0);
    }];
    
    //背景view
    [self.areaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(50);
        make.bottom.mas_equalTo(self).offset(-50);
        make.center.mas_equalTo(self);
        make.width.mas_equalTo(300);
    }];
    
    //头像
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(100);
        make.left.mas_equalTo(self.areaView.mas_left).offset(0);
        make.centerY.mas_equalTo(self);
    }];
    
    //名称
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(5);
        make.top.mas_equalTo(self.areaView).offset(10);
    }];
    
    //星级
    [self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(120);
        make.height.mas_equalTo(25);
        make.leading.mas_equalTo(self.nameLabel);
        make.centerY.mas_equalTo(self.areaView);
    }];
    
    //分数标签
    [self.pointLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.areaView).offset(-10);
    }];
    
    //分数
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.pointLabel.mas_right).offset(10);
        make.centerY.mas_equalTo(self.pointLabel);
    }];
    
    //签到按钮
    [self.signInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(60);
        make.top.mas_equalTo(self.areaView).offset(10);
        make.right.mas_equalTo(self.areaView).offset(-10);
    }];
    
    //等级名称
    [self.gradeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.signInButton);
        make.top.mas_equalTo(self.signInButton.mas_bottom).offset(15);
        
    }];
    
}
#pragma mark - setter
- (void)setModel:(XZInfoModel *)model{
    _model = model;
    self.nameLabel.text = model.Name;
    self.gradeNameLabel.text = model.GradeName;
    self.scoreLabel.text = model.Coin;
    //头像
    [self.iconImageView yy_setImageWithURL:[NSURL URLWithString:model.HeadUrl] placeholder:DefealtImg];
    
    //是否已经签到
    if (!model.IsDaySign) {
        _signInButton.userInteractionEnabled = YES;
        [_signInButton setTitle:@"签到" forState:UIControlStateNormal];
        [_signInButton setBackgroundColor:[UIColor colorWithHexString:@"FF8700" andAlpha:1]];
    }else{
        _signInButton.userInteractionEnabled = NO;
        [_signInButton setTitle:@"已签到" forState:UIControlStateNormal];
        [_signInButton setBackgroundColor:[UIColor colorWithHexString:@"878787" andAlpha:1]];
    }
    
    //皇冠 太阳 月亮 星星 图片
    NSInteger crownCount = model.GradeIconCount.CrownCount;
    NSInteger sunCount = model.GradeIconCount.SunCount;
    NSInteger moothCount = model.GradeIconCount.MoonCount;
    NSInteger starCount = model.GradeIconCount.StarCount;
    [self.levelView setCrownCount:crownCount SunCount:sunCount MoonCount:moothCount StarCount:starCount];
    NSInteger total = crownCount + sunCount + moothCount + starCount;
    [self.levelView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20*total);
    }];

}

#pragma mark - buttonClick
- (void)signInButtonClick:(UIButton *)sender{//签到
    if (self.delegate && [self.delegate respondsToSelector:@selector(showSignView:)]) {
        [self.delegate showSignView:@"SIGN"];
    }
}
- (void)goInfoController{//点击头像
    if (self.delegate && [self.delegate respondsToSelector:@selector(showSignView:)]) {
        [self.delegate showSignView:@"ICON"];
    }
}
#pragma mark - getter
- (UIImageView *)backImg{
    if (!_backImg) {
        _backImg = [[UIImageView alloc]init];
        _backImg.contentMode = UIViewContentModeScaleAspectFill;
        _backImg.image = [UIImage imageNamed:@"headViewBackImg"];
    }
    return _backImg;
}
- (UIView *)areaView{
    if (!_areaView) {
        _areaView = [[UIView alloc]init];
        _areaView.backgroundColor = [UIColor colorWithHexString:@"234E66" andAlpha:0.7];
        
        //指定边切圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 300, 100) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(50,50)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _areaView.bounds;
        maskLayer.path = maskPath.CGPath;
        _areaView.layer.mask = maskLayer;
    }
    return _areaView;
}
- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];
//        _iconImageView.contentMode = UIViewContentModeScaleToFill;
        _iconImageView.layer.cornerRadius = 50.0f;
        _iconImageView.clipsToBounds = YES;
    }
    return _iconImageView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel sizeToFit];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = [UIFont boldSystemFontOfSize:14.0f];;
    }
    return _nameLabel;
}
-(UIImageView *)gradeImageView{
    if (!_gradeImageView) {
        _gradeImageView = [[UIImageView alloc]init];
        _gradeImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _gradeImageView;
}
- (UILabel *)pointLabel{
    if (!_pointLabel) {
        _pointLabel = [[UILabel alloc]init];
        _pointLabel.text = @"学币";
        _pointLabel.textColor = [UIColor whiteColor];
        _pointLabel.textAlignment = NSTextAlignmentLeft;
        _pointLabel.font = [UIFont systemFontOfSize:14.0f];
    }
    return _pointLabel;
}
- (UILabel *)scoreLabel{
    if (!_scoreLabel) {
        _scoreLabel = [[UILabel alloc]init];
        _scoreLabel.textColor = [UIColor colorWithHexString:@"FF8700" andAlpha:1];
        _scoreLabel.textAlignment = NSTextAlignmentLeft;
        _scoreLabel.font = [UIFont systemFontOfSize:14.0f];
    }
    return _scoreLabel;
}

- (UIButton *)signInButton{
    if (!_signInButton) {
        _signInButton = [[UIButton alloc]init];
        [_signInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _signInButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        _signInButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        _signInButton.layer.cornerRadius = 5.f;
        _signInButton.clipsToBounds = YES;
        [_signInButton addTarget:self action:@selector(signInButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _signInButton;
}

- (UILabel *)gradeNameLabel{
    if (!_gradeNameLabel) {
        _gradeNameLabel = [[UILabel alloc]init];
        _gradeNameLabel.textColor = [UIColor whiteColor];
        _gradeNameLabel.textAlignment = NSTextAlignmentRight;
        _gradeNameLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    }
    return _gradeNameLabel;
}
- (GMlevelView *)levelView{
    if (!_levelView) {
        _levelView = [[GMlevelView alloc]init];
        _levelView.backgroundColor = [UIColor clearColor];
    }
    return _levelView;
}

@end
